﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;
using GameLogic;

namespace GameEditor
{
    public class CustomImportSettings : AssetPostprocessor
    {
        const int MAX_TEXTURE_SIZE = 2048;

        public static bool enabled = true;

        private static Regex MAP_TEXTURE = new Regex(@"Map/[\d]*/");

        private static TextureImporterSettings importerSettings = new TextureImporterSettings();
        /// <summary>
        /// 图片处理
        /// </summary>
        private static Dictionary<string, Action<TextureImporter, string>> handlers = new Dictionary<string, Action<TextureImporter, string>>
        {
            //地图
            {"Assets/ManagedResources/Map/", SetMapTexture},
            //UI
            {"Assets/RawResources/UIAtlas/", SetUITexture},
            //装备&怪物&NPC
            {"Assets/RawResources/Prefabs/Equips/", SetMultipleSprite},
            {"Assets/RawResources/Prefabs/Monsters/", SetMultipleSprite},
            {"Assets/RawResources/Prefabs/Npcs/", SetMultipleSprite},

            //PlayerMode
            {"Assets/RawResources/Prefabs/PlayerModel/ChiBang/", SetMultipleSprite},
            {"Assets/RawResources/Prefabs/PlayerModel/ShenJie/", SetMultipleSprite},
            {"Assets/RawResources/Prefabs/PlayerModel/WuQi/", SetMultipleSprite},
            {"Assets/RawResources/Prefabs/PlayerModel/YiFu/", SetMultipleSprite},
            {"Assets/RawResources/Prefabs/PlayerModel/ZuoQi/", SetMultipleSprite}
        };


        void OnPreprocessTexture()
        {
            if (!enabled) return;
            TextureImporter importer = assetImporter as TextureImporter;
            UpdateTextureSetting(importer, assetPath);
        }
        /// <summary>
        /// 地图图片设置图片压缩格式
        /// </summary>
        static void SetMapTexture(TextureImporter importer,string path)
        {
            importer.spritePackingTag = string.Empty;
            importer.SetTextureSettingsExt(false,TextureImporterType.Sprite, 1, AppConst.PIXELTOWORLD, false, TextureWrapMode.Clamp, FilterMode.Bilinear, TextureImporterNPOTScale.None);
            if (importer.name.Contains("_mkey") || importer.name.Contains("_radar")) //大地图
            {
                importer.SetPlatformSettingsExt("Android", TextureImporterFormat.ETC_RGB4, MAX_TEXTURE_SIZE,50, false);
                importer.SetPlatformSettingsExt("iPhone", TextureImporterFormat.PVRTC_RGB4, MAX_TEXTURE_SIZE, 50, false);
                importer.SetPlatformSettingsExt("Standalone", TextureImporterFormat.RGB24, MAX_TEXTURE_SIZE, 50, false);
            }
            else  //瓦片图片,不透明
            {
                importer.SetPlatformSettingsExt("Android", TextureImporterFormat.ETC_RGB4, MAX_TEXTURE_SIZE, 50, false);
                importer.SetPlatformSettingsExt("iPhone", TextureImporterFormat.PVRTC_RGB4, MAX_TEXTURE_SIZE, 50, false);
                importer.SetPlatformSettingsExt("Standalone", TextureImporterFormat.RGB24, MAX_TEXTURE_SIZE, 50, false);
            }
        }

        /// <summary>
        /// 设置UI图片自动图集名称,与图片格式
        /// </summary>
        static void SetUITexture(TextureImporter importer,string path)
        {
            importer.spritePackingTag = GetAtlasName(importer.assetPath,path);
            importer.SetTextureSettingsExt(true,TextureImporterType.Sprite, 1, AppConst.PIXELTOWORLD, false, TextureWrapMode.Clamp, FilterMode.Trilinear, TextureImporterNPOTScale.None);
            importer.SetPlatformSettingsExt("Android", TextureImporterFormat.ETC2_RGBA8, MAX_TEXTURE_SIZE, 50, false);
            importer.SetPlatformSettingsExt("iPhone", TextureImporterFormat.PVRTC_RGBA4, MAX_TEXTURE_SIZE, 50, false);
            importer.SetPlatformSettingsExt("Standalone", TextureImporterFormat.RGBA32, MAX_TEXTURE_SIZE, 50, false);
        }

        /// <summary>
        /// 设置SpritePacker打包好的图集
        /// </summary>
        /// <param name="importer"></param>
        /// <param name="path"></param>
        static void SetMultipleSprite(TextureImporter importer, string path) {
            importer.spritePackingTag = string.Empty;
            importer.SetTextureSettingsExt(true,TextureImporterType.Sprite, 2, AppConst.PIXELTOWORLD, false, TextureWrapMode.Clamp, FilterMode.Trilinear, TextureImporterNPOTScale.None);
            importer.SetPlatformSettingsExt("Android", TextureImporterFormat.ETC2_RGBA8, MAX_TEXTURE_SIZE, 50, false);
            importer.SetPlatformSettingsExt("iPhone", TextureImporterFormat.PVRTC_RGBA4, MAX_TEXTURE_SIZE, 50, false);
            importer.SetPlatformSettingsExt("Standalone", TextureImporterFormat.DXT5, MAX_TEXTURE_SIZE, 50, false);
        }

        static void SetPlayerMode(TextureImporter importer,string path) {
            string spritePackingTag = string.Empty;
            if (path.Contains("YiFu"))
            {
                spritePackingTag = "UIYiFu";
            }
            else
            {
                spritePackingTag = GetAtlasName(importer.assetPath, path);
            }
            importer.spritePackingTag = spritePackingTag;
            importer.SetTextureSettingsExt(true,TextureImporterType.Sprite, 2, AppConst.PIXELTOWORLD, false, TextureWrapMode.Clamp, FilterMode.Trilinear, TextureImporterNPOTScale.None);
            importer.SetPlatformSettingsExt("Android", TextureImporterFormat.ETC2_RGBA8, MAX_TEXTURE_SIZE, 50, false);
            importer.SetPlatformSettingsExt("iPhone", TextureImporterFormat.PVRTC_RGBA4, MAX_TEXTURE_SIZE, 50, false);
            importer.SetPlatformSettingsExt("Standalone", TextureImporterFormat.DXT5, MAX_TEXTURE_SIZE, 50, false);
        }

        public static string GetAtlasName(string assetPath, string path) {
            string atlasName = assetPath.Replace(path, string.Empty);
            int index = atlasName.IndexOf("/");
            if (index != -1)
            {
                atlasName = atlasName.Substring(0, index);
            }
            return atlasName;
        }

        /// <summary>
        /// 更新一张图片设置
        /// </summary>
        public static void UpdateTextureSetting(TextureImporter importer, string assetPath)
        {
            foreach (var each in handlers) {
                if (importer.assetPath.Contains(each.Key))
                {
                    each.Value(importer, each.Key);
                    break;
                }
            }
        }
    }


    public static class  ImporterExt{
        /// <summary>
        /// 设置Texture参数
        /// </summary>
        /// <param name="importer"></param>
        /// <param name="type"></param>
        /// <param name="spriteMode"></param>
        /// <param name="spritePixelsPerUnit"></param>
        /// <param name="mipmapEnabled"></param>
        /// <param name="wrapMode"></param>
        /// <param name="filterMode"></param>
        /// <param name="noptScale"></param>
        public static void SetTextureSettingsExt(this TextureImporter importer,bool alphaIsTransparency, TextureImporterType type, int spriteMode, float spritePixelsPerUnit, bool mipmapEnabled, TextureWrapMode wrapMode, FilterMode filterMode, TextureImporterNPOTScale noptScale)
        {
            TextureImporterSettings importerSettings = new TextureImporterSettings();
            importer.ReadTextureSettings(importerSettings);
            importerSettings.npotScale = noptScale;
            importerSettings.spriteMode = spriteMode;
            importerSettings.spritePixelsPerUnit = spritePixelsPerUnit;
            importerSettings.mipmapEnabled = mipmapEnabled;
            importerSettings.wrapMode = wrapMode;
            importerSettings.filterMode = filterMode;
            importerSettings.alphaIsTransparency = alphaIsTransparency;
            importer.SetTextureSettings(importerSettings);
        }

        /// <summary>
        /// 设置平台参数
        /// </summary>
        /// <param name="importer"></param>
        /// <param name="platform"></param>
        /// <param name="format"></param>
        /// <param name="maxSize"></param>
        /// <param name="compressionQuality"></param>
        /// <param name="allowsAlphaSplitting"></param>
        public static void SetPlatformSettingsExt(this TextureImporter importer, string platform, TextureImporterFormat format, int maxSize, int compressionQuality, bool allowsAlphaSplitting)
        {
            TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings();
            settings.name = platform;
            settings.maxTextureSize = maxSize;
            settings.format = format;
            settings.compressionQuality = compressionQuality;
            settings.allowsAlphaSplitting = allowsAlphaSplitting;
            settings.overridden = true;
            importer.SetPlatformTextureSettings(settings);
        }
    }
}
